from time import sleep

import requests
import hashlib
import time
# https://m.shengqingqy.cn/pages/users/index
from datetime import datetime
from buy import buy
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from sendEmil import send_email

import webbrowser


token = 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOiJoNV91c2VyOjE4OTUxMzc4NjI1MzgxNDE2OTgiLCJyblN0ciI6IlRwa1pGakZleWVmNGxWT1lPWFpaUlc3MFo5d0pkWDJ4In0.2zacNYGdr2iv1hI9lNFITvo8NNHA9shyJO-oiPWDYeo'
token2 = 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOiJoNV91c2VyOjE4OTUxMzgxNzAxODY4ODcxNjkiLCJyblN0ciI6Ims1SG9Rd1NEY0trdkxlN01LUmFwWkVVWW9iMzBxWlY0In0.oKHMq7uHkTtGy0WOS3fp2ngHCO-Baaq2ExVA5k-WEhU'

productids = [
	{'name':'桃馨','id':'1897935417556537346'},
	{'name':'丁婉','id':'1897955344478687233'},
	{'name':'霄汉独角','id':'1898633128146042881'},
	{'name':'2025九灵狮神','id':'1897914801224732674'},
	{'name':'云岳哮','id':'1898939780997623809'},
	{'name':'青龙','id':'1898730386835095554'},
	{'name':'白虎','id':'1898672318043582465'},
	{'name':'朱雀','id':'1898337390046199810'},
	{'name':'逸风客','id':'1897890009453084674'},
	{'name':'光头','id':'1899707708548382721'},
	{'name':'雄狮女王','id':'1898987876106309634'},

]



while True:
	# time.sleep(2)
	# # 获取当前时间
	now=datetime.now()
	# # 格式化时间为时分秒
	current_time = now.strftime("%H:%M:%S")
	# # 打印时分秒
	print("----------------------------------------当前时间:", current_time)
	for product_id in productids:
		# time.sleep(0.4)
		headers = {
				'Accept': 'application/json, text/plain, */*',
				'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
				'Authorization':token2,
				'Cache-Control': 'no-cache',
				'Connection': 'keep-alive',
				'Origin': 'https://m.shengqingqy.cn',
				'Pragma': 'no-cache',
				'Referer': 'https://m.shengqingqy.cn/',
				'Sec-Fetch-Dest': 'empty',
				'Sec-Fetch-Mode': 'cors',
				'Sec-Fetch-Site': 'same-site',
				'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1',
				'content-language': 'zh-CN',
				# 'token': '813233226662f4a6da739b92e60271de',
		}

		params = {
				'pageNum': '1',
				'pageSize': '20',
				'type': '1',
				'key': '',
				'status': '',
				'sortType': '4',
				'productId': product_id['id'],
				'sign_appid': 'h5_23514861',
				'timestamp': now,
				# 'nonce': 'aavkv5244011UhGpB',
				# 'sign': '450a77975f57bad28d4971a4065c0d04',
		}
		try:
			response = requests.get('https://api.shengqingqy.cn/market/app/market/list', params=params, headers=headers).json()
			# print(response)
			list = response['rows']
			# print(list)
			if len(list) < 4:
				print(f"商品 {product_id['name']} 没有足够的商品信息")
				continue
			print(f"商品: {list[0]['name']}, 价格: {list[0]['price']} / {list[1]['price']} / {list[2]['price']}")
			# 判断价格差异改为50%
			if float(list[0]['price']) <= float(list[1]['price']) * 0.4:
				res = buy(list[0]['id'],token)
				print('res:::', res)
				# print((res['code']) == 500)
				if res['code'] == 200:
					content = (f'商品: {list[0]["name"]}\n'
					           f'价格: {list[0]["price"]}\n'
					           f'商品列表 {list[1]["price"]}\n'
					           f'商品列表 {list[2]["price"]}\n'
					           f'https://m.shengqingqy.cn/order/list'
					           )
					send_email('青龙来咯', content)
					print('https://m.shengqingqy.cn/order/list')
					url = "https://www.douyin.com/user/self?from_tab_name=main&modal_id=7190605186274954536&showTab=like"
					webbrowser.open(url)
					exit()
		except Exception as e:
			# print(f'检查商品 {product_id} 时发生错误:', e)
			continue








  # Note: json_data will not be serialized by requests
  # exactly as it was in the original request.
  # data = '{"productId":"1938819","randomCode":"66151f36a0d77fe9ceab6e263bbd04bf","type":"3","count":"1","captchaTicket":"","captchaRandstr":"","score":0,"firstBuyId":"","ruleId":"","payMethod":"4","sign_appid":"h5_23514861","timestamp":1741163899,"nonce":"qyQsw8781896pGtMT","sign":"8735e1ad2e3204776e2fdb8ea472eb17"}'
  # response = requests.post('https://api.shengqingqy.cn/order/app/order/submit', headers=headers, data=data)